from django.shortcuts import render

# Create your views here.
from django.views import View
from .verify_pic import generate_captcha_text,generate_captcha_image
from django_redis import get_redis_connection
from . import constants
from django.http import HttpResponse
import logging

# 日志记录器
logger = logging.getLogger('django')


class ImageCodeView(View):
    def get(self, request, uuid):
        text = generate_captcha_text()
        image = generate_captcha_image(text)
        # print(text)  # 输出生成的验证码
        redis_conn = get_redis_connection('verify_code')  # 保存图形验证码
        # setex 保存到redis中 并设置生存时间
        redis_conn.setex('img_%s' % uuid,
                         constants.IMAGE_CODE_REDIS_EXPIRES, text)
        # 响应图形验证码
        return HttpResponse(image, content_type='image/jpg')

